SPORE.$Import('core.element.event');

/*
---

name: DOMReady

description: Contains the custom event domready.

license: MIT-style license.

requires: [Browser, Element, Element.Event]

provides: [DOMReady, DomReady]

...
*/

SPORE.register('core.domready',function($ns){

	(function(window, document){
	
	var ready,
		loaded,
		checks = [],
		shouldPoll,
		timer,
		testElement = document.createElement('div');
	
	var domready = function(){
		clearTimeout(timer);
		if (ready) return;
		Browser.loaded = ready = true;
		document.removeListener('DOMContentLoaded', domready).removeListener('readystatechange', check);
		
		document.fireEvent('domready');
		window.fireEvent('domready');
	};
	
	var check = function(){
		for (var i = checks.length; i--;) if (checks[i]()){
			domready();
			return true;
		}
		return false;
	};
	
	var poll = function(){
		clearTimeout(timer);
		if (!check()) timer = setTimeout(poll, 10);
	};
	
	document.addListener('DOMContentLoaded', domready);
	
	/*<ltIE8>*/
	// doScroll technique by Diego Perini http://javascript.nwbox.com/IEContentLoaded/
	// testElement.doScroll() throws when the DOM is not ready, only in the top window
	var doScrollWorks = function(){
		try {
			testElement.doScroll();
			return true;
		} catch (e){}
		return false;
	}
	// If doScroll works already, it can't be used to determine domready
	//   e.g. in an iframe
	if (testElement.doScroll && !doScrollWorks()){
		checks.push(doScrollWorks);
		shouldPoll = true;
	}
	/*</ltIE8>*/
	
	if (document.readyState) checks.push(function(){
		var state = document.readyState;
		return (state == 'loaded' || state == 'complete');
	});
	
	if ('onreadystatechange' in document) document.addListener('readystatechange', check);
	else shouldPoll = true;
	
	if (shouldPoll) poll();
	
	Element.Events.domready = {
		onAdd: function(fn){
			if (ready) fn.call(this);
		}
	};
	
	// Make sure that domready fires before load
	Element.Events.load = {
		base: 'load',
		onAdd: function(fn){
			if (loaded && this == window) fn.call(this);
		},
		condition: function(){
			if (this == window){
				domready();
				delete Element.Events.load;
			}
			return true;
		}
	};
	
	// This is based on the custom load event
	window.addEvent('load', function(){
		loaded = true;
	});
	
	})(window, document);

});